
#pragma once

#include <string>
#include <functional>

class Connection;
class Loop_Epollserver;

using fun_t = std::function<void(std::shared_ptr<Connection>)>;

// 用Connection类来管理没有接收和发送缓冲区
class Connection
{
public:
    Connection(int sockfd, std::shared_ptr<Loop_Epollserver> loop_svr, fun_t recv, fun_t sender, fun_t except)
        : _sockfd(sockfd),
          _loop_svr(loop_svr),
          _recv(recv),
          _sender(sender), _except(except)
    {
    }

    int Get_fd()
    {
        return _sockfd;
    }

    void add_indata(const std::string &data)
    {
        _inbuffer += data;
    }

    void add_outdata(const std::string &data)
    {
        _outbuffer += data;
    }

    std::weak_ptr<Loop_Epollserver>& Get_loopsvr()
    {
        return _loop_svr;
    }

    std::string &Inbuffer()
    {
        return _inbuffer;
    }

    std::string &Outbuffer()
    {
        return _outbuffer;
    }

private:
    int _sockfd;

    std::string _inbuffer;  // 输入缓冲区
    std::string _outbuffer; // 输出缓冲区
    std::weak_ptr<Loop_Epollserver> _loop_svr;

public:
    fun_t _recv;
    fun_t _sender;
    fun_t _except;
};